<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>

      <el-row :gutter="20">
        <el-col :span="8">
            <el-input placeholder="请输入权限名搜索"
                    v-model="searchRight"
                    clearable
                    @clear="getRights"
          >
            <el-button slot="append" icon="el-icon-search" @click="getRights"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addRight">添加权限</el-button>
        </el-col>
      </el-row>

      <el-table :data="rightList" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="权限名" prop="menuName"></el-table-column>
        <el-table-column label="路径" prop="path"></el-table-column>
        <el-table-column label="权限等级" prop="menuLevel">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.menuLevel === 1">一级</el-tag>
            <el-tag type="success" v-else="scope.row.menuLevel === 2">二级</el-tag>

          </template>
        </el-table-column>

        <el-table-column label="操作" style="display: inline-block;" width="190px">
        <template slot-scope="scope">
          <el-tooltip effect="dark" content="修改权限信息" placement="top" :enterable="false">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.menuId)"></el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="删除权限" placement="top" :enterable="false">
            <el-button type="danger" icon="el-icon-delete" @click="deleteMenu(scope.row.menuId)"
                       size="mini"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog
      title="添加权限"
      :visible.sync="addDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="addDialogClosed">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="80px">
        <el-form-item label="权限名称" prop="menuName">
          <el-input v-model="addForm.menuName"></el-input>
        </el-form-item>
        <el-form-item label="父级权限" prop="parentName">
          <el-select v-model="parentName" clearable placeholder="请选择" @change="parentMenuSelect">
            <el-option
              v-for="item in parentMenusList"
              :key="item.menuId"
              :label="item.menuName"
              :value="item.menuId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限路径" prop="path">
          <el-input v-model="addForm.path"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitAddMenu()">确 定</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="修改权限信息"
      :visible.sync="editDialogVisible"
      width="50%"
      :close-on-click-modal="false"
      @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="80px">

          <el-form-item label="权限名称" prop="editForm.menuName">
            <el-input v-model="editForm.menuName"></el-input>
          </el-form-item>
        <el-form-item label="父级权限" prop="parentName">
          <el-select v-model="parentName" clearable placeholder="请选择" @change="parentMenuSelect">
            <el-option
              v-for="item in parentMenusList"
              :key="item.menuId"
              :label="item.menuName"
              :value="item.menuId"
            >
            </el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="权限路径" prop="editForm.path">
            <el-input v-model="editForm.path"></el-input>
          </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="changeMenu(editForm)">确 定</el-button>
  </span>
    </el-dialog>
  </div>

</template>

<script>
    import {
        addMenu,
        changeMenuById,
        deleteMenuById,
        getAllMenus,
        getMenuById,
        getMenusByLevel
    } from "../../assets/api/menusApi";
    import {deleteUserById} from "../../assets/api/userApi";

    export default {
        name:'rights',
        data(){
            return{
                rightList: [],
                searchRight: '',
                pageNum: 1,
                pageSize: 10,
                total: 0,
                addDialogVisible: false,
                addForm:{},
                parentName: '',
                parentMenusList:'',
                addFormRules: {
                    menuName: [{required: true, message: '请输入权限名称', trigger: 'blur'},
                        {min: 3, max: 10, message: '权限名称的长度在3~10个字符之间', trigger: 'blur'}],
                    path: {required: true, message: '请输入权限路径', trigger: 'blur'}
                },
                parentId:0,
                editDialogVisible:false,
                editForm:{},
                editFormRules:{
                    menuName: [{required: true, message: '请输入权限名称', trigger: 'blur'},
                        {min: 3, max: 10, message: '权限名称的长度在3~10个字符之间', trigger: 'blur'}],
                    path: {required: true, message: '请输入权限路径', trigger: 'blur'}
                },
            }
        },
        methods:{
            showEditDialog(menuId){
                this.editDialogVisible = true;
                let data = {};
                data.menuId = menuId;
                getMenuById(data).then((res)=>{
                    this.editForm = res.data;
                });
                this.getParentMenus();
            },
            getRights(){
                let data = {};
                data.menuName = this.searchRight;
                getAllMenus(this.pageNum,this.pageSize,data).then((res)=>{
                    if (res.code === 200) {
                        this.rightList = res.data.records;
                        this.total = res.data.total;
                    }
                })
            },
            parentMenuSelect(item) {
                this.parentId = item;
                if (item === '') {
                    this.parentId = 0;
                }
            },
            getParentMenus() {
                let data = {};
                data.menuLevel = 1;
                getMenusByLevel(data).then((res) => {
                    if (res.code !== 200) {
                        return this.$message.error(res.message);
                    }
                    this.parentMenusList = res.data;
                })
            },
            handleSizeChange(newSize) {
                this.pageSize = newSize;
                this.getRights();
            },
            handleCurrentChange(newPage) {
                this.pageNum = newPage;
                this.getRights();
            },
            addRight(){
                this.addDialogVisible = true;
                this.getParentMenus();
            },
            addDialogClosed() {
                this.$refs.addFormRef.resetFields();
            },
            submitAddMenu(){
                this.$refs.addFormRef.validate(valid => {
                    if (!valid) return;
                    this.addForm.parentId = this.parentId;
                    if (this.addForm.parentId === 0) {
                        this.addForm.menuLevel = 1;
                    } else {
                        this.addForm.menuLevel = 2;
                    }
                    addMenu(this.addForm).then((res) => {
                        if (res.code !== 200) {
                            return this.$message.error(res.message);
                        }
                        this.$message.success(res.message);
                        this.addDialogVisible = false;
                        this.getRights();
                    })
                })
            },
            editDialogClosed(){
                this.$refs.editFormRef.resetFields();
            },
            changeMenu(){
                if (this.parentName === '' || this.parentName === null){
                    this.editForm.menuLevel = 1;
                    this.editForm.parentId = 0;
                } else {
                    this.editForm.menuLevel = 2;
                    this.editForm.parentId = this.parentName;
                }
                this.$refs.editFormRef.validate(valid=>{
                    if (!valid) return;
                    changeMenuById(this.editForm).then((res)=>{
                        if (res.code !== 200){
                            return this.$message.error(res.message);
                        }
                        this.editDialogVisible = false;
                        this.getRights();
                        this.$message.success(res.message);
                    })
                })
            },
            deleteMenu(menuId) {
                const confirmResult = this.$confirm(
                    '此操作将永久删除该权限, 是否继续?',
                    '提示',
                    {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(()=>{
                    let data = {};
                    data.menuId = menuId;
                    deleteMenuById(data).then((res) => {
                        if (res.code === 200) {
                            this.$message.success(res.message);
                            this.getRights();
                        } else {
                            this.$message.error(res.message);
                        }

                    })
                }).catch(() => {
                    this.$message.info('已取消删除');
                });

            },
        },
        created() {
            this.getRights();
        }


    }
</script>

<style scoped>
  @import "../../styles/css/global.css";

  /deep/ .el-table {
    margin-top: 15px;
    font-size: 12px;
  }

  /deep/ .el-pagination {
    margin-top: 15px;
  }
</style>
